﻿using System;
using System.Collections.Generic;
using System.Linq;
using Socium.Domain.Tests;
using Socium.Presentation;

namespace Socium.TestEditor {
	class ClosedQuestionTreeItem : TreeItemBase<ClosedQuestion, VariantTreeItem> {
		public ClosedQuestionTreeItem(ClosedQuestion question) : base(question) {}

		public override IEnumerable<CommandViewModel> Commands {
			get {
				return new[] {
					new CommandViewModel("Properties...", new Command<IFormViewService>(OnDetails)), 
					new CommandViewModel("New variant...", new Command<IFormViewService>(OnNewVariant)), 
					new CommandViewModel("Delete", new Command<IFormViewService>(OnDelete)), 
				};
			}
		}

		void OnDetails(IFormViewService form_view_service) {
			Details(form_view_service, model => new ClosedQuestionViewModel(model));
		}

		void OnDelete(IFormViewService form_view_service) {
			var test_tree_item = Parent as TestTreeItem;
			if (test_tree_item == null) {
				throw new InvalidOperationException();
			}

			test_tree_item.Childeren.Remove(this);
		}

		protected override string GetDisplayName(ClosedQuestion model) {
			return model.Text;
		}

		protected override IEnumerable<VariantTreeItem> GetInitalChilderen(ClosedQuestion model) {
			return model.Variants.Select(variant => new VariantTreeItem(variant));
		}

		protected override void OnItemAdded(VariantTreeItem item) {
			Model.Variants.Add(item.Model);
		}

		protected override void OnItemRemoved(VariantTreeItem item) {
			Model.Variants.Remove(item.Model);
		}

		private void OnNewVariant(IFormViewService form_view_service) {
			NewChild<VariantViewModel, Variant>(form_view_service, variant => new VariantTreeItem(variant));
		}
	}
}
